package com.MedDroid.entities;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 19/11/13
 * Time: 7:40 PM
 * Description: Provides static methods used to encrypt and decrypt data.
 */
public class Cryptography
{
	static final int Shift = 3;

	/**
	 *Cryptography.class
	 *Provides the encryption and decryption services for the system.
	 *
	 *Encrypt (String word);
	 *Returns a string with the result of encryption being applied on word.
	 *Assumes to operate on a set of characters between '1' and 'z' in unicode
	 *
	 *Decrypt (String word);
	 *Returns a string with the result of decryption being applied on word.
	 *Assumes to operate on a set of characters between '1' and 'z' in unicode
	 */
	public static String decrypt (String word) {
		int wordLength = word.length();

		StringBuilder decryptedWord = new StringBuilder(wordLength);
                char chr;

		for (int i = 0; i < wordLength; i++) {
			chr = word.charAt(i);

			if (chr != 'z')
				decryptedWord.append((char)((int)(chr) + Shift));
			else
				decryptedWord.append('0');		
		}

		return decryptedWord.toString();
	}

	public static String encrypt(String word) {
		int wordLength = word.length();

		StringBuilder encryptedWord = new StringBuilder(wordLength);
                char chr;

		for (int i = 0; i < wordLength; i++) {
			chr = word.charAt(i);
                        Character.getNumericValue(chr);

			if (chr != '0') {
				encryptedWord.append((char)((int)(chr) - Shift));
                        }
			else
				encryptedWord.append('z');
		}

		return encryptedWord.toString();
	}


        /*
        //This is just for testing
        public static void main (String [] args) {
                String word = "Ponies";
                String encryptedWord = Encrypt(word);
                System.out.println( encryptedWord);
                String decryptedWord = Decrypt(word);
                System.out.println( decryptedWord);
                String word2 = Decrypt(encryptedWord);
                System.out.println(word2);

        }*/
}
